package cn.csc.module.member.service.rolemenu;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.member.controller.app.rolemenu.vo.AppMemberRoleMenuCreateReqVO;
import cn.csc.module.member.controller.app.rolemenu.vo.AppMemberRoleMenuExportReqVO;
import cn.csc.module.member.controller.app.rolemenu.vo.AppMemberRoleMenuPageReqVO;
import cn.csc.module.member.controller.app.rolemenu.vo.AppMemberRoleMenuUpdateReqVO;
import cn.csc.module.member.convert.rolemenu.MemberRoleMenuConvert;
import cn.csc.module.member.dal.dataobject.rolemenu.MemberRoleMenuDO;
import cn.csc.module.member.dal.mysql.rolemenu.MemberRoleMenuMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.member.enums.ErrorCodeConstants.ROLE_MENU_NOT_EXISTS;

/**
 * 角色和菜单关联 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class MemberRoleMenuServiceImpl extends ServiceImpl<MemberRoleMenuMapper, MemberRoleMenuDO> implements MemberRoleMenuService {


    @Override
    public BigInteger createRoleMenu(AppMemberRoleMenuCreateReqVO createReqVO) {
        // 插入
        MemberRoleMenuDO roleMenu = MemberRoleMenuConvert.INSTANCE.convert(createReqVO);
        baseMapper.insert(roleMenu);
        // 返回
        return roleMenu.getId();
    }

    @Override
    public void updateRoleMenu(AppMemberRoleMenuUpdateReqVO updateReqVO) {
        // 校验存在
        validateRoleMenuExists(updateReqVO.getId());
        // 更新
        MemberRoleMenuDO updateObj = MemberRoleMenuConvert.INSTANCE.convert(updateReqVO);
        baseMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoleMenu(BigInteger id) {
        // 校验存在
        validateRoleMenuExists(id);
        // 删除
        baseMapper.deleteById(id);
    }

    private void validateRoleMenuExists(BigInteger id) {
        if (baseMapper.selectById(id) == null) {
            throw exception(ROLE_MENU_NOT_EXISTS);
        }
    }

    @Override
    public MemberRoleMenuDO getRoleMenu(BigInteger id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<MemberRoleMenuDO> getRoleMenuList(Collection<BigInteger> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberRoleMenuDO> getRoleMenuPage(AppMemberRoleMenuPageReqVO pageReqVO) {
        return baseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberRoleMenuDO> getRoleMenuList(AppMemberRoleMenuExportReqVO exportReqVO) {
        return baseMapper.selectList(exportReqVO);
    }

}
